package com.test.mybatis.test_mybatis.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.test.mybatis.test_mybatis.entity.simple.Teacher;
import com.test.mybatis.test_mybatis.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author wxb
 * @Datatime 2021/1/14 17:41
 * @Descript
 */
@RestController
@RequestMapping("teacher")
public class TeacherController {

    @Autowired private TeacherService teacherService;

    @GetMapping("/{id}")
    public String selectById(@PathVariable Integer id){
        Teacher teacher = teacherService.selectById(id);
//        System.out.println("teacher...");
        return JSONObject.toJSONString(teacher);
    }

    /**
     * 查询医生列表并关联学生
     * @param page
     * @param rows
     * @return
     */
    @GetMapping("list")
    public String selectList(@RequestParam(name = "page",defaultValue = "1")Integer page,
                             @RequestParam(name = "rows",defaultValue = "2")Integer rows){
        System.out.println("发送访问请求");
        PageHelper.startPage(page,rows);
        List list = teacherService.selectTeachers();
        PageInfo pageInfo = new PageInfo(list);
        return JSONObject.toJSONString(pageInfo);
//        return "执行完成";
    }

    /**
     * 根据请求参数，模糊查询数据
     * @param param
     * @return
     */
    @GetMapping("/param")
    public PageInfo selectByParams(String param,
                                 @RequestParam(name = "page",defaultValue = "1")Integer page,
                                 @RequestParam(name = "rows",defaultValue = "5")Integer rows){
        PageHelper.startPage(page,rows);
        List list = teacherService.selectTeachersByParam(param);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

}
